package com.ndp.fb.walle.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ndp.ec.core.ProductSetService;
import com.ndp.ec.core.model.SamplingFeed;
import com.ndp.fb.enums.ErrorType;
import com.ndp.fb.exception.CheckedException;
import com.ndp.fb.mongo.model.Feed;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.walle.util.JSONUtil;

@RestController
@RequestMapping("/productSet")
public class ProductSetController {

    @Reference
    ProductSetService productSetService;

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(@RequestBody JSONObject productSet, HttpServletRequest req) throws Exception {
        User user = (User) req.getSession().getAttribute("user");
        Map data = new HashMap<>();
        if (productSet != null) {
            productSet.put("user_id", user.getTicket());
            String id = productSetService.createProductSet(productSet);
            data.put("id",id);
            return JSON.toJSONString(data);
        } else {
            throw new CheckedException(ErrorType.PARAM_EXCEPTION);
        }
    }

    @RequestMapping("preview")
    public List<SamplingFeed> samplingMaterialShowProduct(@RequestParam(value = "param", required = true) String  param){
        String productSetId = JSONObject.parseObject(param).getString("product_set_id");
        List<SamplingFeed> list = getSamplingFeeds(productSetId);
        return list;
    }

    @RequestMapping(value = "query", method = RequestMethod.POST)
    public Pager<JSONObject> query(@RequestParam(value = "param", required = true) String param, HttpServletRequest req) throws Exception {
        if (param != null) {
        	String feed_id = JSONObject.parseObject(param).getString("feed_id");
            Pager<JSONObject> result = productSetService.getProductSetPager(feed_id, JSONUtil.getIntetger(param, "cPage", 1), JSONUtil.getIntetger(param, "pSize", 10));
            return result;
        } else {
            throw new CheckedException(ErrorType.PARAM_EXCEPTION);
        }
    }

    @RequestMapping(value = "findByFeed", method = RequestMethod.POST)
    public Collection findProductSetByFeed(@RequestParam(value = "param", required = true) String param) throws CheckedException {
    	if (param != null) {
    		JSONObject pObject = JSONObject.parseObject(param);
    		JSONArray feed_ids = pObject.getJSONArray("feed_ids");
        	String status = pObject.getString("status");
            if (!StringUtils.hasText(status)) status = "finished";
            JSONArray ps = productSetService.getProductSetsByFeed(feed_ids,status);
            Set<JSONObject> collect = ps.stream().map(p -> {
                JSONObject o = (JSONObject) p;
                o.put("name", o.getString("name") + "(" + o.getLong("data_size") + ")");
                return o;
            }).collect(Collectors.toSet());
            return collect;
        } else {
            throw new CheckedException(ErrorType.PARAM_EXCEPTION);
        }
    }

    @RequestMapping(value = "getMeta", method = RequestMethod.POST)
    public JSONObject getMeta(@RequestParam(value = "param", required = true) String param) throws CheckedException {
    	if (param != null) {
    		JSONObject pObject = JSONObject.parseObject(param);
    		JSONObject ps = productSetService.getMeta(pObject);
            return ps;
        } else {
            throw new CheckedException(ErrorType.PARAM_EXCEPTION);
        }
    }
    private List<SamplingFeed> getSamplingFeeds(String pSet_id) {
        List<Feed> feedList = productSetService.getFeedList(pSet_id,0, 100);
        if(CollectionUtils.isEmpty(feedList)){
            return new ArrayList();
        }
        List<SamplingFeed> list=feedList.stream().map(feed-> {
            SamplingFeed sf=new SamplingFeed();
            BeanUtils.copyProperties(feed,sf);
            return sf;
        }).collect(Collectors.toList());
        return list;
    }
}
